﻿using System;
using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
using System.Xml;
using Server.Model;

namespace Server.DB
{
    public class XMLImport
    {
        public static ICollection<ZipCodeCity> LoadZipCodesFromFile(string zipXML)
        {
            List<ZipCodeCity> zips = new List<ZipCodeCity>();
            XmlDocument doc = new XmlDocument();
            doc.Load(zipXML);
            XmlNodeList l = doc.SelectNodes("/Zipcodes/*");
            foreach (XmlNode n in l)
            {
                ZipCodeCity z = new ZipCodeCity() { 
                    ZipCode = int.Parse(n["Zip"].InnerText),
                    City = n["City"].InnerText
                };
                zips.Add(z);
            }

            return zips;
        }
    

   
        
        public static ICollection<Route> LoadRoutesFromXML(string routesXML, ICollection<Station> stations)
        {
            List<Route> list = new List<Route>();
            XmlDocument doc = new XmlDocument();
            doc.Load(routesXML);
            XmlNodeList l = doc.SelectNodes("/RECORDS/*");
            foreach (XmlNode n in l)
            {
                Route r = new Route();
                r.From = GetStationsByID(int.Parse(n["location1"].InnerText),stations);
                r.To = GetStationsByID(int.Parse(n["location2"].InnerText), stations);
                r.Distance = Double.Parse(n["distance"].InnerText.Replace(".", ","));
                r.Time =  (int.Parse(n["time"].InnerText.Substring(0, 2)) * 60 * 60) + 
                    (int.Parse(n["time"].InnerText.Substring(3, 2)) * 60) + 
                    (int.Parse(n["time"].InnerText.Substring(6, 2)));
                list.Add(r);
            }

            return list;
        }

        private static Station GetStationsByID(int id, ICollection<Station> stations)
        {
            IEnumerator<Station> Inum = stations.GetEnumerator();
            bool found = false;
            while (!found && Inum.MoveNext())
            {
                found = Inum.Current.Id.Equals(id);
            }
            if (found)
            {
                return Inum.Current;
            }
            else
            {
                throw new Exception("Station not found");
            }
        }

        public static ICollection<Station> LoadStationsFromXML(string StationsXML)
        {
            List<Station> list = new List<Station>();
            XmlDocument doc = new XmlDocument();
            doc.Load(StationsXML);
            XmlNodeList l = doc.SelectNodes("/RECORDS/*");
            foreach (XmlNode n in l)
            {
                if (n["type"].InnerText == "3")
                {
                    Station v = new Station();
                    v.Name = n["name"].InnerText;
                    v.StreetAddress = n["streetaddress"].InnerText;
                    v.Id = int.Parse( n["id"].InnerText);
                    v.City = n["city"].InnerText;
                    v.Latitude = Double.Parse(n["latitude"].InnerText.Replace(".", ","));
                    v.Longitude = Double.Parse(n["longitude"].InnerText.Replace(".", ","));
                    v.ZipCode = n["zipcode"].InnerText;
                    list.Add(v);
                }
            }
            return list;
        }
    }
}
